package com.kermel.andromeda.activities.countdown.add;

import android.app.Activity;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;

import com.kermel.andromeda.R;
import com.kermel.andromeda.data.CountdownTimer;
import com.kermel.common.util.RingtoneUtils;
import com.kermel.common.util.StringUtils;

public class AddCountdownTimerActivity extends Activity {
    private TimePicker editCountdownTimePicker;
    private CountdownTimer countdownTimer;
    
    private TextView editCountdownSoundTitleDisplay;
    private View editCountdownStartButton;
    private CheckBox editCountdownPlaySoundInput;
    private CheckBox editCountdownVibrateInput;
    
    public AddCountdownTimerActivity() {
        countdownTimer = new CountdownTimer();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.edit_countdown);
        
        storeWidgetHandles();
        
        initListeners();
        
        updateDisplayBasedOnValuesFromCountdownTimer();
        
        adjustTimePicker();
    }
    
    private void storeWidgetHandles() {
        editCountdownTimePicker = (TimePicker)findViewById(R.id.editCountdownTimePicker);
        editCountdownStartButton = findViewById(R.id.editCountdownStartButton);
        editCountdownVibrateInput = (CheckBox)findViewById(R.id.editCountdownVibrateInput);
        editCountdownPlaySoundInput = (CheckBox)findViewById(R.id.editCountdownPlaySoundInput);
        editCountdownSoundTitleDisplay = (TextView)findViewById(R.id.editCountdownSoundTitleDisplay);
    }
    
    private void initListeners() {
        PlaySoundCheckedHandler playSoundCheckedHandler = new PlaySoundCheckedHandler();
        editCountdownPlaySoundInput.setOnCheckedChangeListener(playSoundCheckedHandler);
        
        VibrateDeviceCheckedHandler vibrateDeviceCheckedHandler = new VibrateDeviceCheckedHandler();
        editCountdownVibrateInput.setOnCheckedChangeListener(vibrateDeviceCheckedHandler);
        
        DoneButtonAction doneButtonAction = new DoneButtonAction();
        editCountdownStartButton.setOnClickListener(doneButtonAction);
    }
    
    private void updateDisplayBasedOnValuesFromCountdownTimer() {
        if (countdownTimer != null) {
            updateVibrateDeviceInputBasedOnValueFromCountdownTimer();
            determineStartButtonEnablement();
        }
    }
    
    private void updateVibrateDeviceInputBasedOnValueFromCountdownTimer() {
        editCountdownVibrateInput.setChecked(countdownTimer.vibrate);
    }
    
    private void adjustTimePicker() {
        editCountdownTimePicker.setIs24HourView(true);
        editCountdownTimePicker.setCurrentHour(0);
        editCountdownTimePicker.setCurrentMinute(1);
    }
    
    private void selectSound() {
        RingtoneUtils.promptForRingtoneSelection(this);
    }
    
    private void onDone() {
        gatherValuesFromWidgets();
        calculateCountdownTimerTriggerTime();
        launchCountdownTimerActivity();
    }
    
    private void calculateCountdownTimerTriggerTime() {
        countdownTimer.calculateTriggerTime();
    }
    
    private void gatherValuesFromWidgets() {
        gatherHourAndMinute();
    }
    
    private void gatherHourAndMinute() {
        countdownTimer.hoursFromNow = editCountdownTimePicker.getCurrentHour();
        countdownTimer.minutesFromNow = editCountdownTimePicker.getCurrentMinute();
    }
    
    private Intent launchCountdownTimerActivity() {
        Intent intent = new Intent(this, CountdownTimerActivity.class);

        intent.putExtra(CountdownTimer.COUNTDOWN_TIMER_PARCEL_NAME, countdownTimer);
        startActivityForResult(intent, CountdownTimerActivity.COUNTDOWN_TIMER_ACTIVITY_RESULT_ID);
        
        return intent;        
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (resultCode == RESULT_OK) {
            Uri ringtoneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            acceptSound(ringtoneUri);
            determineStartButtonEnablement();
        }
    }
    
    private void acceptSound(Uri ringtoneUri) {
        countdownTimer.acceptRingtone(this, ringtoneUri);
        updateSoundTitleDisplay(countdownTimer.ringtoneTitle);
    }
    
    private void updateSoundTitleDisplay(String title) {
        editCountdownSoundTitleDisplay.setText(title);
    }
    
    private void clearSound() {
        countdownTimer.clearRingtone();
        clearSoundTitleDisplay();
    }
    
    private void clearSoundTitleDisplay() {
        editCountdownSoundTitleDisplay.setText(StringUtils.EMPTY);
    }
    
    private void determineStartButtonEnablement() {
        boolean enabled = eitherPlaySoundOrVibrateHasBeenSet();
        editCountdownStartButton.setEnabled(enabled);
    }
    
    private boolean eitherPlaySoundOrVibrateHasBeenSet() {
        if (countdownTimer.hasRingtone() || countdownTimer.vibrate)
            return true;
        
        return false;
    }
    
    private class DoneButtonAction implements View.OnClickListener {
        public void onClick(View v) {
            onDone();
        }
    }
    
    private class PlaySoundCheckedHandler implements OnCheckedChangeListener {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                selectSound();
            else
                clearSound();
            
            determineStartButtonEnablement();
        }
    }
    
    private class VibrateDeviceCheckedHandler implements OnCheckedChangeListener {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            countdownTimer.vibrate = isChecked;
            
            determineStartButtonEnablement();
        }
    }
    
}
